﻿

var InsertCalculatorInsurance = function (partnerElement, insuranceElement, dateBeginElement, dateEndElement, countNightElement,
    priceAdultElement, priceChildElement, quantityAdultElement, quantityChildElement, vatElement,
    totalElement, tourIdElement, priceBabyElement, quantityBabyElement, noteElement,userIdElement) {

    //ham tinh tong gia
    var fncTotalPriceInsurance = function() {
        var priceAdult = $(priceAdultElement).val();
        var priceChild = $(priceChildElement).val();
        var priceBaby= $(priceBabyElement).val();
        var quantityAdult = $(quantityAdultElement).val();
        var quantityChild = $(quantityChildElement).val();
        var quantityBaby = $(quantityBabyElement).val();
        var vat = $(vatElement).val();
        var totalprice = 0;
        if (priceAdult > 0 && priceChild > 0 ) {
            totalprice = (priceAdult * quantityAdult) + (priceChild * quantityChild) + (priceBaby * quantityBaby);
            vat =  totalprice * 0.1;
        }
        $(totalElement).text(totalprice);
        $(vatElement).val(vat);
    };
    //ham load danh sach bao hiem
    var fncLoadInsurance = function(partner,partnerName) {
        var partnerId = $(partnerElement).val();
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "InsuranceByPartner", "PartnerId": partnerId },
            dataType: "json",
            success: function(data) {
                $(insuranceElement).html('<option value="-1">--- Chọn bảo hiểm ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function(index, item) {
                        $(insuranceElement)
                            .append($("<option></option>")
                                .attr("value", item.ID)
                                .text(item.Name));
                    });
                }

                $(insuranceElement).combobox('setValue', partnerName);
                $(insuranceElement).val(partner);
            }
        });
    };
    $(partnerElement).combobox({
        select: function (event, ui) {
            fncLoadInsurance();
        }
    });
    // ham tinh so ngay
    var fncCountNight = function () {
        if ($(dateBeginElement).val() != "" && $(dateEndElement).val() != "") {
            var dateBegin = $(dateBeginElement).val().split("/");
            var dateEnd = $(dateEndElement).val().split("/");
            var firstDate = new Date(dateBegin[2], dateBegin[1], dateBegin[0]);
            var secondDate = new Date(dateEnd[2], dateEnd[1], dateEnd[0]);
            var oneDay = 24 * 60 * 60 * 1000;
            var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime()) / oneDay)+1;
            
            $(countNightElement).text(diffDays);
        }
    };
    $(dateBeginElement).change(fncCountNight);
    $(dateEndElement).change(fncCountNight);
    //ham load gia bao hiem
    var fncLoadPriceInsurance = function() {
        var insuranceId = $(insuranceElement).val();
        var dateBegin = $(dateBeginElement).val();
        $(priceChildElement).val('0');
        $(priceAdultElement).val('0');
        $(priceBabyElement).val('0');
        $('#btnInsertInsurance').attr('price', '-1');
        if (insuranceId > 0 && dateBegin != NaN) {
            $.ajax({
                type: "POST",
                url: "/ajax_mh.html",
                data: { "function": "PriceInsurance", "InsuranceId": insuranceId, "DateBegin": dateBegin },
                dataType: "json",
                success: function(data) {
                    if (data && data.isError == false) {
                        $(priceAdultElement).val(data.PriceAdult);
                        $(priceChildElement).val(data.PriceChild);
                        $(priceBabyElement).val(data.PriceBaby);
                        $('#btnInsertInsurance').attr('price', data.PriceId);
                    }
                    fncTotalPriceInsurance();
                }
            });
        }
    };
    $(insuranceElement).combobox({
        select: function (event, ui) {
            fncLoadPriceInsurance();
        }
    });
    $(dateBeginElement).change(fncLoadPriceInsurance);
    
    $(quantityAdultElement).keyup(fncTotalPriceInsurance);
    $(quantityChildElement).keyup(fncTotalPriceInsurance);
    $(quantityBabyElement).keyup(fncTotalPriceInsurance);
    $(priceBabyElement).keyup(fncTotalPriceInsurance);
    //ham them sua xoa
    var fncCalculatorInsurance = function() {
        var typeId = 7;
        var tourId = parseInt(tourIdElement);
        var ticketId = $(this).attr('price');

        var dateBegin = $(dateBeginElement).val();
        var dateEnd = $(dateEndElement).val();
        var quantityAdult = $(quantityAdultElement).val();
        var quantityChild = $(quantityChildElement).val();
        var quantityBaby = $(quantityBabyElement).val();
        var priceAdult = $(priceAdultElement).val();
        var priceChild = $(priceChildElement).val();
        var priceBaby = $(priceBabyElement).val();
        var vat = $(vatElement).val();
        var note = $(noteElement).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        var concern = $(partnerElement).val();
        var userId = parseInt(userIdElement);
        $.ajax({
            type: "POST",
            url: "/ajax_vmh.html",
            data: {
                "function": "InsertInsurance", "TypeId": typeId, "TourId": tourId, "TicketId": ticketId, "DateBegin": dateBegin,
                "DateEnd": dateEnd, "QuantityAdult": quantityAdult, "QuantityChild": quantityChild, "PriceAdult": priceAdult,
                "PriceChild": priceChild, "VAT": vat, "CalculatorId": calculatorId, "ClassName": className, "PriceBaby": priceBaby,
                "QuantityBaby": quantityBaby, 'Note': note, 'Concern': concern, 'UserId': userId
            },
            dataType: "json",
            success: function(data) {

                $(".ListInsurance").remove();
                $(partnerElement).val('-1');
                $(dateBeginElement).val('');
                $(dateEndElement).val('');
                $(countNightElement).text('');
                $(insuranceElement + ' > option').remove();
                $(priceAdultElement).val('');
                $(priceChildElement).val('');
                $(priceBabyElement).val('');
                $(quantityAdultElement).val('');
                $(quantityChildElement).val('');
                $(quantityBabyElement).val('');
                $(vatElement).val('');
                $(totalElement).text('');
                $(noteElement).val('');
                if (data && data.isError == false) {
                    $.each(data.array, function(index, item) {
                        $('#new-ticket-insurance')
                            .before($("<tr class='ListInsurance'></tr>").attr("id", 'Insurance-' + item.CalculatorId).html('<td></td>' +
                                '<td >' + item.PartnerName + '</td>' +
                                '<td >' + item.InsuranceName + '</td>' +
                                '<td >' + item.BeginDate + '</td>' +
                                '<td >' + item.EndDate + '</td>' +
                                '<td >' + item.QuantityDate + '</td>' +
                                '<td >' + item.PriceAdult + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.PriceBaby + '</td>' +
                                '<td >' + item.QuantityAdult + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td >' + item.QuantityBaby + '</td>' +
                                
                                '<td >' + item.TotalPrice + '</td>' +
                                '<td>' + item.VAT + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateInsurance" onclick="openPopup(\'ticket-insurance\')"  type="button" value="" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteInsurance"  type="button" value="" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));

                    });
                    $('#TotalInsurancePrices').html(data.TotalInsurancePrices);
                    $('#TotalInsurancePricesAdults').html(data.TotalInsurancePricesAdults);
                    $('#TotalInsurancePricesChild').html(data.TotalInsurancePricesChild);
                    $('#TotalInsurancePricesBaby').html(data.TotalInsurancePricesBaby);
                    $('#TotalVatInsurance').html(data.TotalVatInsurance);
                    $('#btnInsertInsurance').attr("calculatorid", "-1").val("Thêm");
                    TotalPrice();
                    fncTotalCalculator();
                    hidePopup('ticket-insurance');
                }

            }
        });
        
    };

    $('#btnInsertInsurance').on("click", fncCalculatorInsurance);
    $('#btnDeleteInsurance').on("click", fncCalculatorInsurance);    
    $('table').on("click", 'tr >td >input.btnDeleteInsurance', fncCalculatorInsurance);        
    $('table').on("click", 'tr >td >input.btnUpdateInsurance', function () {
        var calculatorId = $(this).attr("calculatorid");
        
        $.ajax({
            type: "POST",
            url: "/ajax_vmh.html",
            data: { "function": "UpdateCalculatorInsurance", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {
                $('#Insurance-' + calculatorId).remove();

                $(partnerElement).combobox('setValue', data.PartnerName);
                $(partnerElement).val(data.PartnerId);
                fncLoadInsurance(data.InsuranceId, data.InsuranceName);

                $(dateBeginElement).val(data.DateBegin);
                $(dateEndElement).val(data.DateEnd);
                $(countNightElement).text(data.QuantityDate);
                $(quantityAdultElement).val(data.QuantityAdult);
                $(quantityChildElement).val(data.QuantityChild);
                $(quantityBabyElement).val(data.QuantityBaby);
                $(priceAdultElement).val(data.PriceAdult);
                $(priceChildElement).val(data.PriceChild);
                $(priceBabyElement).val(data.PriceBaby);
                $(vatElement).val(data.Vat);
                $(totalElement).text(data.TotalPrice);
                $(noteElement).val(data.Note);

                $('#btnInsertInsurance').attr("calculatorid", calculatorId).attr('price',data.PriceId).val("Lưu");
                    
                    
            }
        });
    });
}